package com.jsu.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.jsu.bean.Found;
import com.jsu.bean.FoundDetail;
import com.jsu.bean.FoundMessage;
import com.jsu.service.RecieveFoundService;
import com.jsu.service.UsrService;


@RestController
public class RecieveFoundController {

	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
    String basicDir = "E:\\test\\";
    
    @Autowired
    UsrService usrService;
    @Autowired
    RecieveFoundService recieveFoundService;
    //上传街拍
	@RequestMapping(value = "/recieveFound", method = RequestMethod.POST)
	public void RecieveFound(MultipartHttpServletRequest request){  
	
	    Found found = new Found();
	    FoundDetail foundDetail = new FoundDetail();
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("photo");
        for(MultipartFile file:files){
        	System.out.println("===================================");
        }
        String username=request.getParameter("username");
        found.setProvider(usrService.getIdByName(username));
        found.setLatitude(Float.parseFloat(request.getParameter("latitude")));
        found.setLongitude(Float.parseFloat(request.getParameter("longitude")));
        found.setPhototime(new Date());
        String foundposition=request.getParameter("foundposition");
        if(foundposition!=null&&(!foundposition.trim().equals(""))){
        	found.setFoundposition(foundposition);
        }
        String description=request.getParameter("description");
        if(description!=null&&(!description.trim().equals(""))){
        	found.setDescription(description);
        }
        foundDetail.setFound(found);
        MultipartFile file = null;    
        BufferedOutputStream stream = null;    
        for (int i = 0; i < files.size(); ++i) { 
            file = files.get(i);    
            if (!file.isEmpty()) {    
                try {
                	java.util.Date dt = new Date();
                	String sDateTime = sdf.format(dt);
                    byte[] bytes = file.getBytes();    
                    stream = new BufferedOutputStream(new FileOutputStream(    
                            new File(basicDir+sDateTime+".jpg"))); 
                    stream.write(bytes);    
                    stream.close();
                    FoundMessage foundMessage = new FoundMessage();
                    foundMessage.setPhotopath(sDateTime+".jpg");
                    foundDetail.addFoundMessage(foundMessage);
                    for(int j=0;j<100000;j++);
                } catch (Exception e) {    
                    stream = null;    
                    
            } 
        }    

	}
        recieveFoundService.recieveFound(foundDetail);
}
}